/*
 * Copyright 2007-2009 Thomas Krause (thomas@berlin-reloaded.de)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gidoo.cms.helper;

import org.apache.wicket.authorization.strategies.role.Roles;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.gidoo.cms.admin.AdminPage;
import org.gidoo.cms.editor.EditorPage;
import org.gidoo.cms.login.GidooCMSSession;
import org.gidoo.cms.publisher.PublisherPage;

/**
 * A helper panel that shows links to the administration functionalities the user
 * may access.
 * 
 * @author <a href="mailto:thomas@berlin-reloaded.de>Thomas Krause</a>
 */
public class AdminNavPanel extends Panel
{

  /**
   * Constructor.
   * @param id The wicket ID.
   * @param parentPageClass Wether the parent is a admin-, edit- or publish-page.
   */
  public AdminNavPanel(String id, Class parentPageClass)
  {
    super(id);

    Link lnkAdmin = new Link("lnkAdmin")
    {

      @Override
      public void onClick()
      {
        setResponsePage(AdminPage.class);
      }
    };
    
    add(lnkAdmin);
    
    Link lnkEdit = new Link("lnkEdit")
    {

      @Override
      public void onClick()
      {
        setResponsePage(EditorPage.class);
      }
    };
    
    add(lnkEdit);
    
    Link lnkPublish = new Link("lnkPublish")
    {

      @Override
      public void onClick()
      {
        setResponsePage(PublisherPage.class);
      }
    };
    
    add(lnkPublish);
    
    Label lblSeparator1 = new Label("lblSeparator1", new Model(" | "));
    add(lblSeparator1);
    
    Label lblSeparator2 = new Label("lblSeparator2", new Model(" | "));
    add(lblSeparator2);
    
    // set visibility and if enabled //
    
    // link on own page?
    if(parentPageClass == AdminPage.class)
    {
      lnkAdmin.setEnabled(false);
    }
    else if(parentPageClass == EditorPage.class)
    {
      lnkEdit.setEnabled(false);
    }
    else if(parentPageClass == PublisherPage.class)
    {
      lnkPublish.setEnabled(false);
    }
    
    // access allowed?
    GidooCMSSession session = (GidooCMSSession) getSession();
    Roles r = session.getRoles();
    
    if(r.hasRole(Roles.ADMIN))
    {
      lnkAdmin.setVisible(true);
    }
    else
    {
      lnkAdmin.setVisible(false);
    }
    
    if(r.hasRole(GidooCMSSession.ROLE_EDITOR))
    {
      lnkEdit.setVisible(true);
    }
    else
    {
      lnkEdit.setVisible(false);
    }
    
    if(r.hasRole(GidooCMSSession.ROLE_PUBLISHER))
    {
      lnkPublish.setVisible(true);
    }
    else
    {
      lnkPublish.setVisible(false);
    }
    
    // set separator visibility
    lblSeparator1.setVisible(false);
    lblSeparator2.setVisible(false);
    
    if(
      r.hasRole(Roles.ADMIN) && r.hasRole(GidooCMSSession.ROLE_EDITOR) 
      && r.hasRole(GidooCMSSession.ROLE_PUBLISHER)
      )
    {
      lblSeparator1.setVisible(true);
      lblSeparator2.setVisible(true);
    }
    else if(r.hasRole(Roles.ADMIN) && r.hasRole(GidooCMSSession.ROLE_EDITOR))
    {
      lblSeparator1.setVisible(true);
    }
    else if(r.hasRole(GidooCMSSession.ROLE_EDITOR) && r.hasRole(GidooCMSSession.ROLE_PUBLISHER))
    {
      lblSeparator2.setVisible(true);
    }
    else if(r.hasRole(Roles.ADMIN) && r.hasRole(GidooCMSSession.ROLE_PUBLISHER))
    {
      lblSeparator1.setVisible(true);
    }
  }
}
